package com.inspur.icity.bus.base.core.service;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * [云片网络]短信服务
 */
@Service("YunPianService")
public class YunPianService {
    Logger logger = LoggerFactory.getLogger(getClass());

    private static String URI_SEND_SMS = "http://yunpian.com/v1/sms/send.json";
    private static String ENCODING = "UTF-8";
    private static String APIKEY = "9b92bc512ec082bc439e313a19f1e505"; // E

    public static String SIGNATURE = "【e高速】";
    public static String SIGNATURE_SDHSG = "【山东高速-e高速】";

    /**
     * 通用接口发短信
     *
     * @param text   　短信内容
     * @param mobile 　接受的手机号
     * @return json格式字符串
     */
    public String sendSms(String mobile, String text) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", APIKEY);
        params.put("mobile", mobile);
        params.put("text", text);
        String returnCode = post(URI_SEND_SMS, params);
        logger.debug(returnCode);
        return returnCode;
    }

    /**
     * 基于HttpClient 4.3的通用POST方法
     *
     * @param url       提交的URL
     * @param paramsMap 提交<参数，值>Map
     * @return 提交响应
     */
    public String post(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(),e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(e.getMessage(),e);
            }
        }
        logger.debug(responseText);
        return responseText;
    }

}
